<?php
/* Created by: Ignas Janickas
 * Date: 2011-12-07
 * Class works with works
 */
class Work {
    var $link,$data;
    public $id;
    
    /*
     * Constructor gets data linked with work id
     * @param - work id
     * @ptype - integer
     * 
     * Constructor creates new entry in db and gets data about it
     * @param1 - user id which did work
     * @ptype1 - integer
     * @param2 - category id of work
     * @ptype2 - integer
     * @param3 - work description
     * @ptype3 - string
     * @param4 - date of work
     * @ptype4 - date / string
     */
    function __construct($id, $catid = null, $desc = null, $date = null) {
        require_once("db.class.php");
        include("mysql.config.php");
        $this->link = new DataBase($db_address, $db_username, $db_password, $db_db);
     
        if (($catid != null) && ($desc != null) && ($date != null)) {
            //$this->link->insert("works", null, null);
            $this->link->query("INSERT INTO `works`() VALUES()");
            $this->id = mysql_insert_id();
            $this->setUserId($id);
            $this->setCategoryId($catid);
            $this->setDescription($desc);
            $this->setDate($date);
        } else {
            $this->id = $id;
        }//if $catid and $desc <> null
        
        $this->update(); //sets $data.
    }//construct
    
    /*6
     * Function updates $data param
     */
    private function update() {
        $this->link->select("works","*","id='$this->id'");
        $this->data = $this->link->getResult();
    }//update
    
    /*
     * Function sets timeout to TRUE
     * @param - 1 = true 0 = false
     * @ptype - integer;
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function isTimeout($set = null) {
        if ($set == null) {
            return $this->data['timeout'];
        } else {
            $res = $this->link->query("UPDATE `works` SET `timeout` = '$set' WHERE `id` = '$this->id'");
            if ($res) {
                $this->update();
                return 1;
            } else
                return 0;
        }
    }//isTimeout
    
    /*
     * Function sets User ID Which Did Work
     * @param - user id
     * @ptype - integer
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setUserId($uid) {
        $res = $this->link->query("UPDATE `works` SET `uid` = '$uid' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else
            return 0;
    }//setUserId
    
    /*
     * Function sets Category ID Of Work
     * @param - category id
     * @ptype - integer
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setCategoryId($catid) {
        $res = $this->link->query("UPDATE `works` SET `catid` = '$catid' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else 
            return 0;
    }//setCategoryId
    
    /*
     * Function sets Work Description
     * @param - description
     * @ptype - string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setDescription($desc) {
        $res = $this->link->query("UPDATE `works` SET `description` = '$desc' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else 
            return 0;
    }//setDescription
    
    /*
     * Function sets Work Date
     * @param - date
     * @ptype - date / string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setDate($date) {
        $res = $this->link->query("UPDATE `works` SET `date` = '$date' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else 
            return 0;
    }//setDate
    
    /*
     * Function gets Work ID
     * @return - returns work id
     * @rtype - integer
     */
    function getId() {
        return $this->id;
    }//getId
    
    /*
     * Function gets User ID Which Did Wokr
     * @return - returns user id which did work
     * @rtype - integer
     */
    function getUserId() {
        return $this->data['uid'];
    }//getUserId
    
    /*
     * Function gets Category ID Of Work
     * @return - returns category id of work
     * @rtype - integer
     */
    function getCategoryId() {
        return $this->data['catid'];
    }//getCategoryId
    
    /*
     * Function gets Work Description
     * @return - returns work description
     * @rtype - string
     */
    function getDescription() {
        return $this->data['description'];
    }//getDescription
    
    /*
     * Function gets Work Date
     * @return - returns work date
     * @rtype - date / string
     */
    function getDate() {
        return $this->data['date'];
    }//getDate
}//class Work
?>